/*
 * Copyright (C) 2004, Velocityme Partnership
 * AdvancedSearchActionForm.java
 *
 * Created on 23 December 2004, 03:07
 */

package com.velocityme.www.actionforms;

import com.velocityme.interfaces.ContactablePK;
import com.velocityme.interfaces.StatusPK;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class AdvancedSearchActionForm extends ActionForm {
    
    private Integer m_nodeId;
    private String m_ids;
    private String m_keywords;
    private Integer[] m_statusIds;
    private Integer[] m_contactableIds;
    private Boolean m_includeStart;
    private Boolean m_includeStop;
    private int m_startYear;
    private int m_startMonth;
    private int m_startDay;
    private int m_stopYear;
    private int m_stopMonth;
    private int m_stopDay;
    
    /** Creates a new instance of AdvancedSearchActionForm */
    public AdvancedSearchActionForm() {
    }
    
    public void reset(ActionMapping mapping, javax.servlet.http.HttpServletRequest request) {
        m_statusIds = new Integer[0];
        m_contactableIds = new Integer[0];
        m_includeStart = new Boolean(false);
        m_includeStop = new Boolean(false);
    }
    
    public Integer getNodeId() { return m_nodeId; }
    public void setNodeId(Integer nodeId) { m_nodeId = nodeId; }
    
    public String getIds() { return m_ids; }
    public void setIds(String ids) { m_ids = ids; }
    
    public String getKeywords() { return m_keywords; }
    public void setKeywords(String keywords) { m_keywords = keywords; }
    
    public Integer[] getStatusIds() { return m_statusIds; }
    public void setStatusIds(Integer[] statusIds) { m_statusIds = statusIds; }
    
    public Integer[] getContactableIds() { return m_contactableIds; }
    public void setContactableIds(Integer[] contactableIds) { m_contactableIds = contactableIds; }
    
    public Boolean getIncludeStart() { return m_includeStart; }
    public void setIncludeStart(Boolean includeStart) { m_includeStart = includeStart; }
    
    public Boolean getIncludeStop() { return m_includeStop; }
    public void setIncludeStop(Boolean includeStop) { m_includeStop = includeStop; }
    
    public int getStartYear() { return m_startYear; }
    public void setStartYear(int year) { m_startYear = year; }
    public int getStartMonth() { return m_startMonth; }
    public void setStartMonth(int month) { m_startMonth = month; }
    public int getStartDay() { return m_startDay; }
    public void setStartDay(int day) { m_startDay = day; }
    public int getStopYear() { return m_stopYear; }
    public void setStopYear(int year) { m_stopYear = year; }
    public int getStopMonth() { return m_stopMonth; }
    public void setStopMonth(int month) { m_stopMonth = month; }
    public int getStopDay() { return m_stopDay; }
    public void setStopDay(int day) { m_stopDay = day; }    
    
    public Integer[] getIdArray() {
        if (m_ids.length() > 0) {
            String[] numbers = Pattern.compile("\\D+").split(m_ids);
            Integer[] ids = new Integer[numbers.length];
            for (int i = 0; i < numbers.length; i++) {
                ids[i] = Integer.decode(numbers[i]);
            }
            return ids;
        }
        else
            return new Integer[0];
    }
    
    public String[] getKeywordsArray() {
        if (m_keywords.length() > 0) {
            return Pattern.compile("\\s+").split(m_keywords);
        }
        else
            return new String[0];
    }
    
    public Date getStartDate() {
        if (m_includeStart.equals(Boolean.TRUE)) {
            Calendar startDate = Calendar.getInstance(); startDate.clear();
            startDate.set(m_startYear, m_startMonth, m_startDay);
            return startDate.getTime();
        }
        else
            return null;
    }
    
    public Date getStopDate() {
        if (m_includeStop.equals(Boolean.TRUE)) {
            Calendar stopDate = Calendar.getInstance(); stopDate.clear();
            stopDate.set(m_stopYear, m_stopMonth, m_stopDay, 23, 59, 59);
            return stopDate.getTime();
        }
        else
            return null;
    }
    
    public StatusPK[] getStatusPKs() {
        StatusPK[] statusPKs = new StatusPK[m_statusIds.length];
        for (int i = 0; i < m_statusIds.length; i++) {
            statusPKs[i] = new StatusPK(m_statusIds[i]);
        }
        return statusPKs;
    }
    
    public ContactablePK[] getContactablePKs() {
        ContactablePK[] contactablePKs = new ContactablePK[m_contactableIds.length];
        for (int i = 0; i < m_contactableIds.length; i++) {
            contactablePKs[i] = new ContactablePK(m_contactableIds[i]);
        }
        return contactablePKs;
    }
    
}
